package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.StudentScoreDao;
import com.example.domain.StudentScore;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.StudentScoreService;
import com.example.tool.DateTool;

@Service
public class StudentScoreServiceImpl implements StudentScoreService {

    @Autowired
    StudentScoreDao studentScoreDao;

    /**
     * @describe 添加学生分数信息
     */
    @Override
    public Result addStudentScore(StudentScore studentScore) {
        if (studentScore != null) {
            // 获取系统当前时间作为记录创建时间
            studentScore.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        StudentScore studentScore1 = studentScoreDao.save(studentScore);

        if (studentScore1 != null) {
            return ResultFactory.buildSuccessResult(studentScore1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新学生分数信息
     */
    @Override
    public Result updateStudentScore(StudentScore studentScore) {
        if (studentScore != null) {
            // 获取系统当前时间作为记录更新时间
            studentScore.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        StudentScore studentScore1 = studentScoreDao.saveAndFlush(studentScore);

        if (studentScore1 != null) {
            return ResultFactory.buildSuccessResult(studentScore1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学生分数编号删除学生分数信息
     */
    @Override
    public Result deleteStudentScoreById(String studentScoreId) {
        int row = studentScoreDao.deleteByStudentScoreId(Integer.parseInt(studentScoreId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学生分数信息
     */
    @Override
    public Result queryAllStudentScore() {
        List<StudentScore> list = studentScoreDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询学生分数信息
     */
    @Override
    public Result queryStudentScoreById(String studentScoreId) {
        List<StudentScore> list = studentScoreDao.findByStudentScoreId(Integer.parseInt(studentScoreId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加学生分数信息
     */
    @Override
    public Result addStudentScoreByExcel(List<StudentScore> list) {
        List<StudentScore> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = studentScoreDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学生分数编号批量删除学生分数信息
     */
    @Override
    public Result deleteStudentScoreByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = studentScoreDao.deleteByStudentScoreIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 学生分数信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        return null;
        /*
         * List<Map<String, Object>> list = null;
         *
         *
         * if (list != null) { return ResultFactory.buildSuccessResult(list); } else {
         * return ResultFactory.buildDefaultFailResult(); }
         */
    }

}
